<?php

namespace app\admin\model\oil;

use app\admin\model\sumprint\Printer;
use think\Model;
use traits\model\SoftDelete;

class Store extends Model
{
    //油站信息使用小油桶车队(立减版)的油站信息
    protected $connection = 'database.db_config_lj';

    use SoftDelete;

    // 表名
    protected $name = 'oil_store';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'images_url',
        'printer_ids_list'
    ];

    public function getStatusTextAttr($value, $data)
    {
        return $data['status']  == '1' ? '启用中' : '禁用中';
    }
    public function getImagesUrlAttr($value, $data)
    {
        $imagesArray = [];
        if (!empty($data['images'])) {
            $imagesArray = explode(',', $data['images']);
            foreach ($imagesArray as &$v) {
                $v = cdnurl($v, true);
            }
            return $imagesArray;
        }
        return $imagesArray;
    }
    public function scopeShow($query) {
        return $query->where('status', 1);
    }

    public function printer () {
        return $this->belongsTo(Printer::class, 'printer_id')->field('id,title');
    }
    public function fuel()
    {
        return $this->hasMany(\app\admin\model\oil\Fuel::class, 'store_id', 'id');
    }
    public function getPrinterIdsListAttr($value, $data)
    {
        $list=Printer::where('status','normal')->column('title','id');
        return $list;
    }

}
